package com.guanyong.services;

import com.guanyong.dao.KeyBaseDaoMapper;
import com.guanyong.dao.KeyBaseJavaDaoMapper;
import com.guanyong.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by mxk on 2017/7/3.
 */
@Service
public class KeybaseDetailsService {

    @Autowired
    KeyBaseDaoMapper keyBaseDaoMapper;
    @Autowired
    KeyBaseJavaDaoMapper keyBaseJavaDaoMapper;


    private  static String dateEnd="2017-09-28";
    private  static String dateEnd2="2017-09-29";

    public List<KeyWeiboZhishuCountModel> getKeyWeiboZhishuById(int id) {
        SimpleDateFormat dft = new SimpleDateFormat("MM.dd");
        Date beginDate = new Date();
        //前一天
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 31);
        String dateStr=dft.format(date.getTime());
        String tempDate = dft.format(beginDate);
        List<String> dateList = new ArrayList<String>();
        dateList.add(tempDate);
        try {
            while (!tempDate.equals(dateStr)){
                date.setTime(dft.parse(tempDate));
                date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
                tempDate = dft.format(date.getTime());
                dateList.add(tempDate);
            }
            dateList = dateList.subList(1,dateList.size()-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        Collections.reverse(dateList);


        List<KeyWeiboZhishuModel> list = keyBaseDaoMapper.getWeiboZhishuById(id);
        List<KeyWeiboZhishuCountModel> listCount = new ArrayList<KeyWeiboZhishuCountModel>();
//        Map<Integer,KeyWeiboZhishuCountModel> keyWeiboZhishuMap = new HashMap<Integer,KeyWeiboZhishuCountModel>();
//        for (KeyWeiboZhishuModel keyWeiboZhishuModel:list){
//            if (keyWeiboZhishuMap.get(keyWeiboZhishuModel.getBaseId())==null){
//                KeyWeiboZhishuCountModel keyWeiboZhishuCountModel = new KeyWeiboZhishuCountModel();
//                keyWeiboZhishuCountModel.setBaseId(keyWeiboZhishuModel.getBaseId());
//                keyWeiboZhishuCountModel.setName(keyWeiboZhishuModel.getName());
//                keyWeiboZhishuMap.put(keyWeiboZhishuModel.getBaseId(),keyWeiboZhishuCountModel);
//            }
//            List<String> temp = keyWeiboZhishuMap.get(keyWeiboZhishuModel.getBaseId()).getTrentList();
//            if (temp.size()<31){
//                temp.add(keyWeiboZhishuModel.getTrent());
//            }else {
//                break;
//            }
//
//        }
//        for (Integer key : keyWeiboZhishuMap.keySet()) {
//            keyWeiboZhishuMap.get(key).setDateList(dateList);
//            listCount.add(keyWeiboZhishuMap.get(key));
//        }
        KeyWeiboZhishuCountModel keyWeiboZhishuCountModel = new KeyWeiboZhishuCountModel();
        keyWeiboZhishuCountModel.setDateList(dateList);
        if (list!=null && list.size()>0){

            keyWeiboZhishuCountModel.setBaseId(id);
            keyWeiboZhishuCountModel.setName(list.get(0).getName());
            List<String> temp = keyWeiboZhishuCountModel.getTrentList();
            for (KeyWeiboZhishuModel keyWeiboZhishuModel:list){

                temp.add(keyWeiboZhishuModel.getTrent());
            }

            Collections.reverse(temp);
        }
        listCount.add(keyWeiboZhishuCountModel);
        return listCount;
    }

    public List<KeyPingfenModel> getKeyPingfenByIdDay(int id) {
        return keyBaseDaoMapper.getKeyPingfenByIdDay(id,getNowDate());
    }

    public List<KeyWeiboModel> getKeyWeiboTop5(int id) {
        return keyBaseDaoMapper.getWeiboByIdDay(id,getNowDate());
    }



    public List<KeyBozhuModel> getKeyBozhuTop5(int id) {
        return keyBaseDaoMapper.getBozhuTop5ById(id,getNowDate());
    }




    public List<KeyHuatiModel> getKeyHuatiTop3(int id) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();


//        List<KeyHuatiModel> list =keyBaseDaoMapper.getHuatiByIdList(id, dft.format(beginDate));
        List<KeyHuatiModel> list =keyBaseDaoMapper.getHuatiByIdList(id, "2017-09-29");
        for (KeyHuatiModel keyHuatiModel:list){
            keyHuatiModel.setReadNum(strToNum(keyHuatiModel.getReadNumStr()));
            keyHuatiModel.setDiscussNum(strToNum(keyHuatiModel.getDiscussNumStr()));

        }
        Collections.sort(list);
        if (list.size()>3){
            return list.subList(0,3);
        }
        return list;
    }

    public List<KeyFilmModel> getKeyFilm(int id) {
        List<KeyFilmModel> list = keyBaseDaoMapper.getKeyFilmList(id);
        String firstPlay =keyBaseDaoMapper.getFirtPlay(id);
        for (KeyFilmModel keyFilmModel:list){
            keyFilmModel.setCurDateStr(firstPlay);
        }
        return list;
    }


 



    private int strToNum(String str){
        int num;
        if (str.indexOf("万")>0){
            str=str.substring(0,str.indexOf("万"));
            float temp = Float.parseFloat(str);
            num = Math.round(temp*10)*1000;
        }else if (str.indexOf("亿")>0){
            str=str.substring(0,str.indexOf("亿"));
            float temp = Float.parseFloat(str);
            num = Math.round(temp*10)*10000000;
        }else {
            num=Integer.valueOf(str);
        }
        return num;
    }

    private static String getNowDate(){
        Date date = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateStr =formatter.format(date);
        dateStr+=" 00:00:00";
        return  dateStr;
    }


    public List<KeyPiaofangModel> getPiaofangByIdList(int id) {

        return  keyBaseDaoMapper.getPiaofangById(id);
    }
    public List<KeyPiaofangModel> getLodPiaofangByIdList(int id) {

        return  keyBaseDaoMapper.getLodPiaofangById(id);
    }

    public KeyPiaofangModel getNowPiaofangById(int id) {
        return keyBaseDaoMapper.getNowPiaofangById(id,getNowDate());
    }

    public List<KeyPlayCountModel> getPlayCountById(int id) {

        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate = new Date();
        List<KeyPlayCountModel> list=new ArrayList<KeyPlayCountModel>();
        try {
            beginDate =dft.parse( dft.format(beginDate));
            String dateStr = dft.format(beginDate);
            while (!dateStr.equals(dateEnd2)){
                Calendar date = Calendar.getInstance();
                date.setTime(dft.parse(dateStr));
                date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
                dateStr=dft.format(date.getTime());
                List<KeyPlayModel> temp=keyBaseDaoMapper.getPlayCountById(id,dateStr);
                KeyPlayCountModel keyPlayCountModel = new KeyPlayCountModel();
                keyPlayCountModel.setCurDate(dateStr);
                for (KeyPlayModel keyPlayModel:temp){
                    if (keyPlayModel.getType().equals("2")){
                        keyPlayCountModel.setQqCount(keyPlayModel.getCount());
                    }else if (keyPlayModel.getType().equals("3")){
                        keyPlayCountModel.setYoukuCount(keyPlayModel.getCount());
                    }else if (keyPlayModel.getType().equals("4")){
                        keyPlayCountModel.setIqiyiCount(keyPlayModel.getCount());
                    }
                }
                list.add(keyPlayCountModel);

            }
            Collections.reverse(list);
            return list;
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return list;
    }


    public List<KeyJavaZhishuModel> getZhishuById(int id) {
        List<KeyJavaZhishuModel> list = keyBaseJavaDaoMapper.selectByid(id);
        return list;
    }

    public Map<String,Object> getZhishu30ById(int id) {
        SimpleDateFormat dft = new SimpleDateFormat("MM.dd");
        Date beginDate = new Date();
        //前一天
        Calendar date = Calendar.getInstance();
        date.setTime(beginDate);
        date.set(Calendar.DATE, date.get(Calendar.DATE) - 31);
        String dateStr=dft.format(date.getTime());
        String tempDate = dft.format(beginDate);
        List<String> dateList = new ArrayList<String>();
        dateList.add(tempDate);
        try {
            while (!tempDate.equals(dateStr)){
                date.setTime(dft.parse(tempDate));
                date.set(Calendar.DATE, date.get(Calendar.DATE) - 1);
                tempDate = dft.format(date.getTime());
                dateList.add(tempDate);
            }
            dateList = dateList.subList(1,dateList.size()-1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        List<KeyJavaZhishuModel> list = keyBaseJavaDaoMapper.selectBy30id(id,dateStr);
        Collections.reverse(list);
        Collections.reverse(dateList);
        Map<String,Object> map  = new HashMap<String, Object>();
        map.put("list",list);
        map.put("date",dateList);
        return map;
    }

}
